package entity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.DBUtil;
import util.Validator;

public class MonthlyCard {

	public String id;
	public String carId;
	public String parkId;
	public Integer month;
	public String startDate;
	public String endDate;
	public String createTime;
	
	public List<String[]> query(String[] params) {
		List<Object> list = new ArrayList<>();
		String sql = "select id, carId, parkId, month, startDate, endDate,"
				+ "case when date_format(now(),'%Y-%m-%d') < startDate then '未生效' "
				+ "when date_format(now(),'%Y-%m-%d') > endDate then '已失效' "
				+ "else '正常' end state,"
				+ "createTime from pm_monthly_card where true ";
		if(Validator.isNotEmpty(params[0])){
			sql += " and carId like concat('%',?,'%')";
			list.add(params[0]);
		}
		if(Validator.isNotEmpty(params[1])){
			sql += " and parkId = ?";
			list.add(params[1]);
		}
		try {
			List<String[]> records = DBUtil.query(sql, list.toArray(), 8);
			return records;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return new ArrayList<String[]>();
	}
	
	public boolean save() {
		String sql = "insert into pm_monthly_card(carId, parkId, month, startDate, endDate, createTime)"
				+ " values (?,?,?,?,date_format(date_add(?, interval ? month),'%Y-%m-%d'),?)";
		Object[] paraArray = {carId, parkId, month, startDate, startDate, month, createTime};
		try {
			int count = DBUtil.update(sql, paraArray);
			return count > 0;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return false;
	}

	public boolean load() {
		String sql = "select carId, parkId, month, startDate, endDate, createTime from pm_monthly_card where id = ?";
		Object[] paraArray = {id};
		try {
			String[] record = DBUtil.queryOne(sql, paraArray, 6);
			carId = record[0];
			parkId = record[1];
			month = Integer.valueOf(record[2]);
			startDate = record[3];
			endDate = record[4];
			return true;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return false;
	}
	
	public boolean extend() {
		String sql = "update pm_monthly_card set month = month + ? ,"
				+"endDate = date_format(date_add(?, interval ? month),'%Y-%m-%d') where id = ?";
		Object[] paraArray = {month,endDate,month,id};
		try {
			int count = DBUtil.update(sql, paraArray);
			return count > 0;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return false;
	}
	
	// 检测月卡是否再已生效
	public boolean check(String carId) {
		String sql = "select id,carId, parkId, month, startDate, endDate, createTime from pm_monthly_card "
				+ "where carId = ? and date_format(now(),'%Y-%m-%d') between startDate and endDate";
		Object[] paraArray = {carId};
		try {
			String[] record = DBUtil.queryOne(sql, paraArray, 7);
			if(record == null){
				return false;
			}
			id = record[0];
			carId = record[1];
			parkId = record[2];
			month = Integer.valueOf(record[3]);
			startDate = record[4];
			endDate = record[5];
			return true;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return false;
	}
	
	// 检测月卡是否再已生效
	public static boolean isExist(String carId) {
		String sql = "select id from pm_monthly_card where date_format(now(),'%Y-%m-%d') <= endDate and carId = ? ";
		Object[] paraArray = {carId};
		try {
			String[] record = DBUtil.queryOne(sql, paraArray, 7);
			if(record == null){
				return false;
			}
			return true;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return false;
	}

}
